﻿
/*
 * 修订时间：2016-05-09
 * 修订人：汪建稳
 * 修订版本：V1.0
 * 修订内容：1.给原有方法添加中文注释 2.添加新方法用于绑定枚举数据
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using SD.Interface;
using SD.Entity.Jsons;
using SD.Common;

public static class ResourceDropDownListExtensions
{
    /// <summary>
    /// 绑定DropDownList扩展方法
    /// </summary>
    /// <param name="html">HtmlHelper对象，调用即可，不需要传递 @Html.ResourceDropDownList</param>
    /// <param name="name">控件id和name</param>
    /// <param name="resourceName">数据源名称。在SD.Business.ResourceService里面添加获取数据逻辑方法，SD.DataAccess.Repository里面添加数据库获取数据方法</param>
    /// <param name="selectedValue">指定默认选中项的值</param>
    /// <param name="htmlAttributes">控件的Attributes，添加方式：new{@class="w60 select"}</param>
    /// <returns>MvcHtmlString</returns>
    public static MvcHtmlString ResourceDropDownList(
        this HtmlHelper html, string name, string resourceName, object selectedValue, object htmlAttributes)
    {
        return ResourceDropDownList(html, name, resourceName, selectedValue, htmlAttributes, false);
    }

    /// <summary>
    /// 绑定DropDownList扩展方法，由上面方法调用
    /// </summary>
    /// <param name="html">HtmlHelper对象</param>
    /// <param name="name">控件id和name</param>
    /// <param name="resourceName">数据源名称。在SD.Business.ResourceService里面添加获取数据逻辑方法，SD.DataAccess.Repository里面添加数据库获取数据方法</param>
    /// <param name="selectedValue">指定默认选中项的值</param>
    /// <param name="htmlAttributes">控件的Attributes，添加方式：new{@class="w60 select"}</param>               
    /// <param name="hasAllOption">是否添加“请选择”</param>
    /// <returns>MvcHtmlString</returns>
    public static MvcHtmlString ResourceDropDownList(
        this HtmlHelper html, string name, string resourceName, object selectedValue, object htmlAttributes, bool hasAllOption)
    {
        var resource = ServiceFactory.GetInstance<IResourceService>();
        var rlist = resource.GetResourceByCode(resourceName, hasAllOption);
        SelectList list = new SelectList(rlist, "Value", "Name", selectedValue);
        return html.DropDownList(name, list, htmlAttributes);
    }

    /// <summary>
    /// 绑定DropDownList扩展方法
    /// </summary>
    /// <param name="html">HtmlHelper对象，调用即可，不需要传递 @Html.ResourceDropDownList</param>
    /// <param name="name">控件id和name</param>
    /// <param name="resourceName">数据源名称。在LY.Business.ServiceImpl.ResourceService里面添加获取数据逻辑方法，LY.DataAccess.Repository里面添加数据库获取数据方法</param>
    /// <param name="selectedValue">指定默认选中项的值</param>
    /// <param name="htmlAttributes">控件的Attributes，添加方式：new{@class="w60 select"}</param>
    /// <param name="parentCode">缓存数据支持专属标识，可留空：""</param>
    /// <param name="hasAllOption">是否添加“请选择”</param>
    /// <returns>MvcHtmlString</returns>
    public static MvcHtmlString ResourceDropDownList(
    this HtmlHelper html, string name, string resourceName, object selectedValue, object htmlAttributes, string parentCode, bool hasAllOption)
    {
        var resource = ServiceFactory.GetInstance<IResourceService>();
        var rlist = resource.GetResourceByCode(resourceName, parentCode, hasAllOption);
        SelectList list = new SelectList(rlist, "Value", "Name", selectedValue);
        return html.DropDownList(name, list, htmlAttributes);
    }


    /// <summary>
    /// 绑定DropDownList扩展方法之自定义列表数据
    /// </summary>
    /// <param name="html">HtmlHelper对象，调用即可，不需要传递 @Html.DropDownListEx</param>
    /// <param name="name">控件id和name</param>
    /// <param name="rlist">List<CodeValue>数据源</param>
    /// <param name="selectedValue">指定默认选中项的值</param>
    /// <param name="htmlAttributes">控件的Attributes，添加方式：new{@class="w60 select"}</param>
    /// <returns>MvcHtmlString</returns>
    public static MvcHtmlString DropDownListEx(
        this HtmlHelper html, string name, List<CodeValue> rlist, object selectedValue, object htmlAttributes)
    {
        SelectList list = new SelectList(rlist, "Code", "Value", selectedValue);
        return html.DropDownList(name, list, htmlAttributes);
    }

    /// <summary>
    /// 绑定DropDownList扩展方法之根据传入枚举绑定数据,注意不要绑定没有值的枚举（默认值为枚举项的hashcode）
    /// </summary>
    /// <param name="html">HtmlHelper对象，调用即可，不需要传递 @Html.ResourceDropDownListByEnum</param>
    /// <param name="name">控件id和name</param>
    /// <param name="enumSourse">传入枚举的类型，使用typeof(枚举)传递</param>
    /// <param name="selectedValue">指定默认选中项的值</param>
    /// <param name="htmlAttributes">控件的Attributes，添加方式：new{@class="w60 select"}</param>
    /// <param name="hasAllOption">是否添加一项“请选择...”</param>
    /// <returns>MvcHtmlString</returns>
    public static MvcHtmlString ResourceDropDownListByEnum(
        this HtmlHelper html, string name, Type enumSourse, object selectedValue, object htmlAttributes, bool hasAllOption)
    {
        List<CodeValue> rlist = new List<CodeValue>();
        Type EnumType = enumSourse;
        if (hasAllOption)
        {
            rlist.Add(new CodeValue() { Code = "请选择...", Value = null });
        }
        foreach (string sourseName in System.Enum.GetNames(EnumType))
        {
            rlist.Add(new CodeValue() { Code = sourseName, Value = ((int)System.Enum.Parse(EnumType, sourseName)).ToString() });
        }
        SelectList list = new SelectList(rlist, "Value", "Code", selectedValue);

        return html.DropDownList(name, list, htmlAttributes);
    }



    public static MvcHtmlString ResourceDropDownListEx<T>(
            this HtmlHelper html, string name, List<T> rlist, string code, string value, object selectedValue, SelectListItem firstItem, object htmlAttributes = null)
    {

        if (rlist != null)
        {
            SelectList list = new SelectList(rlist, value, code, selectedValue);
            return html.DropDownList(name, list, htmlAttributes);
        }
        return html.DropDownList(name);
        /*var rlist = resourceName;

        if (rlist != null)
        {
            var sb = new StringBuilder();
            // put a similar foreach here

            foreach (T item in rlist)
            {
                Type type = typeof(T);
                object o1 = type.GetProperty(code).GetValue(item, null);
                object o2 = type.GetProperty(value).GetValue(item, null);
                if (code == "PROPERTY_LABEL1")
                {
                    object o3 = type.GetProperty("PROPERTY_LABEL2").GetValue(item, null);
                    if (o3 != "")
                    {
                        o1 = o1 + "-" + o3;
                    }
                }
                sb.Append("<div class=\"radio  radio-inline\"><label >");
                sb.Append(html.RadioButton(name, o2, selectedValue != null && o2.ToString() == selectedValue.ToString()));
                sb.AppendFormat("  <span class=\"text\">{0}</span></label></div>", o1.ToString());
            }
            return new MvcHtmlString(sb.ToString());

        }
        return new MvcHtmlString("");*/
    }
}
